package io.github.eiot.json.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import io.github.eiot.annotation.FrameField;
import io.github.eiot.codec.NumberUnit;

import java.io.IOException;

/**
 * created by wang007 on 2025/9/27
 */
public class NumberUnitDeserializer extends AbstractNumberUnitDeserializer<NumberUnit> {

    public static final NumberUnitDeserializer INSTANCE = new NumberUnitDeserializer(null);

    protected NumberUnitDeserializer(FrameField frameField) {
        super(frameField);
    }

    @Override
    public NumberUnit deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        FrameField frameField = this.frameField;
        double value = p.getDoubleValue();
        return NumberUnit.from(value, frameField.unit());
    }

    @Override
    protected JsonDeserializer<?> createSelf(FrameField frameField) {
        return new NumberUnitDeserializer(frameField);
    }
}
